Scroll Progress Bar

Files
...Graspty Experts working on this topic...

In C programming, files are used to store and read data from external storage like a hard disk, USB drive, or network. Files are essential for permanent data storage or data sharing between different programs. C provides a standard library that contains functions to work with files. Files can be categorized into two types: text files and binary files.

Text Files:

Text files are human-readable files that store data in plain text format. Each line in a text file represents a record, and data is separated by delimiters like spaces, commas, etc.

Binary Files:

Binary files store data in a format that is not human-readable. They are used to store non-text data like images, audio, video, etc.

File Operations:
The basic file operations in C are:
  • Opening a file: To work with a file, it must be opened using the fopen() function.
  • Reading from a file: To read data from a file, use functions like fscanf() for text files and fread() for binary files.
  • Writing to a file: To write data to a file, use functions like fprintf() for text files and fwrite() for binary files.
  • Closing a file: After finishing file operations, close the file using the fclose() function.
Sample Code with Explanation and Output:

In the following example, will create a text file, write data to it, read the data back, and then close the file.


#include <stdio.h>

int main() {
    FILE *filePointer;
    char data[100];
    int num;

    // Open the file in write mode
    filePointer = fopen("sample.txt", "w");

    // Check if the file was opened successfully
    if (filePointer == NULL) {
        printf("Error opening the file.\n");
        return 1;
    }

    // Write data to the file
    fprintf(filePointer, "Hello, World!\n");
    fprintf(filePointer, "This is a sample text file.\n");
    fprintf(filePointer, "12345\n");

    // Close the file
    fclose(filePointer);

    // Open the file in read mode
    filePointer = fopen("sample.txt", "r");

    // Check if the file was opened successfully
    if (filePointer == NULL) {
        printf("Error opening the file.\n");
        return 1;
    }

    // Read data from the file and print it
    printf("Data from the file:\n");
    while (fscanf(filePointer, "%s", data) != EOF) {
        printf("%s ", data);
    }
    printf("\n");

    // Close the file
    fclose(filePointer);

    return 0;
}
Output:

Data from the file:
Hello, World! This is a sample text file. 12345
Explanation:
  • In the sample code, first open a file named "sample.txt" in write mode using fopen() with the mode "w".
  • If the file is opened successfully, write three lines of data into the file using fprintf().
  • Next, close the file using fclose().
  • Then, open the same file again in read mode using fopen() with the mode "r".
  • If the file is opened successfully, read the data from the file using fscanf() and print it on the screen until the end of the file (EOF) is reached.
  • Finally, close the file again using fclose().
  • Please note that the file "sample.txt" will be created in the same directory as the compiled program. The data written to the file will persist even after the program finishes execution.

What is a C file?


Data

What is the purpose of fopen() in C?


Open

What function is used to read data from a file in C?


fread

What function is used to write data to a file in C?


fwrite

What is the purpose of fclose() in C?


Close